package generator.examples;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.jtestcase.JTestCase;
import org.jtestcase.JTestCaseException;
import org.jtestcase.TestCaseInstance;

public class Test1 extends TestCase {

	/**
	 * JTestCase instance to be used in this example
	 */
	private JTestCase _jtestcase = null;

	/**
	 * Read the XML file with the test data and build the JTestCase instance
	 * 
	 * @param name
	 */
	public Test1(String name) {
		super(name);
		String dataFile = "resources/data-Test1.xml";
		try {
			_jtestcase = new JTestCase(dataFile, "Test1");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (JTestCaseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Main method if you want to run the example from command line
	 * 
	 * @param args command line parameters
	 */
	public static void main(String[] args) {
		junit.textui.TestRunner.run(suite());
	}

	/**
	 * Suite method that collects all test cases
	 * 
	 * @return The test suite
	 */
	public static Test suite() {
		TestSuite retval = new TestSuite();
		retval.addTest(new Test1("testJice1"));
		return (retval);
	}
	

	/**
	 * Tests for the method addition.
	 * 
	 * @throws IOException
	 */
	public final void testJice1() throws IOException {
		final String METHOD = "testJice1";
		if (_jtestcase == null) {
			fail("couldn't read xml definition file");
		}
		Vector<TestCaseInstance> testCases = null;
		try {
			testCases = _jtestcase.getTestCasesInstancesInMethod(METHOD);
		} catch (JTestCaseException e) {
			e.printStackTrace();
			fail("error parsing xml file for calculate method " + METHOD);
		}
		// For each test case
		for (int i = 0; i < testCases.size(); i++) {
			// Retrieve name of test case
			TestCaseInstance testCase = (TestCaseInstance) testCases.elementAt(i);
			try {
				System.out.println("[TestCaseMethod=" + testCase.getMethod()
						+ ", TestCaseName=" + testCase.getTestCaseName() + "]");
				
				// Get hashed params for this test case
				HashMap<String,Object> params = testCase.getTestCaseParams();
	            
				System.out.println("var1=[class=" + params.get("var1").getClass().getName()
					+ "; value=" + params.get("var1").toString() + "]");
	            
	            if (testCase.getTestCaseAssertValues() != null && !testCase.getTestCaseAssertValues().keySet().isEmpty()) {
	            	Iterator<String[]> iterator = testCase.getTestCaseAssertValues().keySet().iterator();
	            	while (iterator.hasNext()) {
	            		 String[] a = iterator.next();
	            		 boolean succeed = testCase.assertTestVariable(a[0], params.get("var1"));
	            		 assertTrue("Assertion unexpectedly failed ["
	            				 + "TestCaseMethod=" + testCase.getMethod()
	            				 + ", TestCaseName=" + testCase.getTestCaseName()
	            				 + ", assert=" + a[0] + "]", succeed);
					}
	            }	            
			} catch (JTestCaseException e) {
				// An error as occurred while processing JTestCase input
				System.err.print("Error executing test case  " + testCase.getTestCaseName());
				e.printStackTrace();
				continue;
			}
		}
	}

}
